#include "abilitylistmodel.h"

#include <QStringList>

AbilityListModel::AbilityListModel(QObject *parent) :
    QAbstractListModel(parent)
{
}

// QAbstractListModel *****************************************
int AbilityListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    return abilities.count();
}

QVariant AbilityListModel::data(const QModelIndex &index, int role) const
{
    if ( role == Qt::DisplayRole )
        return abilities.at(index.row()).toString();
    else if ( role == Qt::EditRole )
        return abilities.at(index.row()).toString();
    else if ( role == Qt::UserRole )
        return abilities.at(index.row()).abUserFormat;
    else
        return QVariant();
}

bool AbilityListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(index)
    Q_UNUSED(value)
    Q_UNUSED(role)
    return false;
}

Qt::ItemFlags AbilityListModel::flags(const QModelIndex &index) const
{
    if ( abilities.at(index.row()).abName.isEmpty() )
        return Qt::NoItemFlags;
    else
        return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

// QAbstractListModel *****************************************

void AbilityListModel::addAbility(QString name, QString categ, QString subcateg, QString userFmt)
{
    AbilityItemModel itm(name, categ, subcateg);
    itm.abUserFormat = userFmt;

    beginInsertRows(QModelIndex(), abilities.count(), abilities.count());
    //if ( insertRow(abilities.count()+1) )
    abilities.append(itm);
    if ( !name.isEmpty() )
        abilMap.insert(name, itm);
    endInsertRows();
}

bool AbilityListModel::removeAt(const QModelIndex & idx)
{
    if ( !idx.isValid() || idx.row() >= abilities.count() )
        return false;

    beginRemoveRows(QModelIndex(), idx.row(), idx.row());
    abilities.removeAt(idx.row());
    endRemoveRows();
    return true;
}

bool AbilityListModel::removeOne(QString name, QString categ, QString subcateg)
{
    Q_UNUSED(name)
    Q_UNUSED(categ)
    Q_UNUSED(subcateg)
    return false;
}

QString AbilityListModel::getAbility(int idx) const
{
    return abilities.at(idx).toString();
}

QString AbilityListModel::getCategory(int idx) const
{
    return abilities.at(idx).abCateg;
}

QString AbilityListModel::getSubCategory(int idx) const
{
    return abilities.at(idx).abSubcateg;
}

QString AbilityListModel::getCategory(QString abName) const
{
    for( int i = 0; i < abilities.count(); ++i )
    {
        if ( abilities.at(i).abName == abName )
            return abilities.at(i).abCateg;
    }

    return "";
}
QString AbilityListModel::getSubCategory(QString abName) const
{
    for( int i = 0; i < abilities.count(); ++i )
    {
        if ( abilities.at(i).abName == abName )
            return abilities.at(i).abSubcateg;
    }

    return "";
}

// SET

bool AbilityListModel::applyFilter(QString filter, AbilityItemModel& item)
{
    if ( filter == "*")
        return true;
    if ( filter == "*I" && item.abCateg == tr("Investigation") )
        return true;
    if ( filter == "*G" && item.abCateg == tr("General") )
        return true;
    if ( filter.startsWith("*I") )
    {
        if ( filter.contains("P") && item.abSubcateg == tr("Interpersonal") )
            return true;
        if ( filter.contains("T") && item.abSubcateg == tr("Technical") )
            return true;
        if ( filter.contains("A") && item.abSubcateg == tr("Academic") )
            return true;
    }
    if ( filter != item.abName )
        return true;
    return false;
}

void AbilityListModel::applyFilter(QString filter)
{
    QList<AbilityItemModel> ab;
    abilMap.clear();

    foreach(AbilityItemModel item, abilities)
    {
        if ( applyFilter(filter, item) )
        {
            ab << item;
            abilMap.insert(item.abName, item);
        }
    }

    abilities = ab;
}


void AbilityListModel::applyFilters(QStringList filters)
{
    QList<AbilityItemModel> ab;
    abilMap.clear();

    foreach(AbilityItemModel item, abilities)
    {
        bool pass = true;
        foreach(QString filter, filters)
        {
            if ( !applyFilter(filter, item) )
            {
                pass = false;
                break;
            }
        }

        if ( pass )
        {
            ab << item;
            abilMap.insert(item.abName, item);
        }
    }

    abilities = ab;
}

AbilityListModel* AbilityListModel::clone() const
{
    AbilityListModel* model = new AbilityListModel(0);
    model->abilities = abilities;
    model->abilMap = abilMap;

    return model;
}


